# Unbind everything to have a clean slate
unbind -aq -Tprefix
unbind -aq -n

# -----------------------------------------------------------------------------
# Mode declaration
# -----------------------------------------------------------------------------

bind -n C-t switchc -Tnormal     # normal mode
bind -Tnormal i switchc -Troot   # insert mode
bind -Tnormal t switchc -Twindow # window mode
bind -Tnormal v copy-mode        # visual mode

# -----------------------------------------------------------------------------
# Misc and Set up
# -----------------------------------------------------------------------------

# Binding for sourcing tmux.conf
bind -Tnormal S source-file .config/tmux/tmux.conf \; switchc -Tnormal

# Lock tmux
bind -Tnormal C-L lock-server

# Change keyboard layout
#------------------------------------------------------------------------------
# TODO: Only run this part if there is no graphical client running. Further, if
# tmux was started in a graphical session, restart it.

# This one talks with an external daemon that triggers the layout change. It is
# intented to be used in the console, i.e without an X or wayland.
#SWAP_LAYOUT="echo swap > /tmp/kbdaemon/kbd_fifo"
#bind -n F12 run $SWAP_LAYOUT

# Automatic set paste and nopaste in vim using the F11 key. But, outside of
# vim, paste normally
# -----------------------------------------------------------------------------
bind -Tnormal p {
	if -F '#{==:#{pane_current_command},vim}' {
		send-keys F11 ; paste ; send-keys F11 ; switchc -Troot
	} {
		paste ; switchc -Troot
	}
}

# Enter command prompt
bind -Tnormal : command-prompt \; switchc -T normal

# Detach
bind -Tnormal d detach

# Prevent unbinded root kepresses from exiting normal mode
bind -Tnormal Any switchc -Tnormal

# -----------------------------------------------------------------------------
# Pane operations
# -----------------------------------------------------------------------------

# Creating new panes
bind -Tnormal c splitw -c '#{pane_current_path}' -h \; switchc -Troot
bind -Tnormal C splitw -c '#{pane_current_path}' -v \; switchc -Troot

# Kill 
bind -Tnormal D {
	kill-pane
	if -F '#{==:#{window_panes},1}' { switchc -Troot } { switchc -Tnormal }
}

# Switching focus
bind -Tnormal j {
	if -F '#{pane_at_bottom}' '' 'selectp -D'
	if -F '#{==:#{window_panes},2}' { switchc -Troot } { switchc -Tnormal }
}
bind -Tnormal k {
	if -F '#{pane_at_top}' '' 'selectp -U'
	if -F '#{==:#{window_panes},2}' { switchc -Troot } { switchc -Tnormal }
}
bind -Tnormal l {
	if -F '#{pane_at_right}' '' 'selectp -R'
	if -F '#{==:#{window_panes},2}' { switchc -Troot } { switchc -Tnormal }
}
bind -Tnormal h {
	if -F '#{pane_at_left}' '' 'selectp -L'
	if -F '#{==:#{window_panes},2}' { switchc -Troot } { switchc -Tnormal }
}

# Swapping panes
bind -Tnormal J {
	if -F '#{pane_at_bottom}' \
	{ switchc -Tnormal } { selectp -D ; swapp -d -s ! ; switchc -Tnormal }
}
bind -Tnormal K {
	if -F '#{pane_at_top}' \
	{ switchc -Tnormal } { selectp -U ; swapp -d -s ! ; switchc -Tnormal }
}
bind -Tnormal L {
	if -F '#{pane_at_right}' \
	{ switchc -Tnormal } { selectp -R ; swapp -d -s ! ; switchc -Tnormal }
}
bind -Tnormal H {
	if -F '#{pane_at_left}' \
	{ switchc -Tnormal } { selectp -L ; swapp -d -s ! ; switchc -Tnormal }
}

# Zoom in and out
bind -Tnormal z resizep -Z \; switchc -Tnormal

# Moving while zoomed
bind -Tnormal n selectp -t + \; resize-pane -Z \; switchc -Tnormal
bind -Tnormal N selectp -t - \; resize-pane -Z \; switchc -Tnormal

# Open pane running a command
bind -Tnormal r command-prompt -I "splitw -h "
bind -Tnormal R command-prompt -I "splitw -v "

# Resizing
RESIZE=10
bind -Tnormal < resizep -L $RESIZE \; switchc -Tnormal
bind -Tnormal > resizep -R $RESIZE \; switchc -Tnormal
bind -Tnormal - resizep -D $RESIZE \; switchc -Tnormal
bind -Tnormal + resizep -U $RESIZE \; switchc -Tnormal

# -----------------------------------------------------------------------------
# Window operations
# -----------------------------------------------------------------------------

bind -Tnormal Q kill-window \; switchc -Tnormal
bind -Twindow c neww -c '#{pane_current_path}' -a \; switchc -Troot
bind -Twindow C neww -c '#{pane_current_path}' -b \; switchc -Troot
bind -Twindow l next \; switchc -Twindow
bind -Twindow h prev \; switchc -Twindow
bind -Twindow L swapw -t + \; next-window \; switchc -Twindow
bind -Twindow H swapw -t - \; previous-window \; switchc -Twindow

# -----------------------------------------------------------------------------
# Visual mode
# -----------------------------------------------------------------------------

# Synchronize tmux paste buffer with the X11 clipboard
bind -Tcopy-mode-vi y {
	send -X copy-selection-and-cancel; 
	run -b 'tmux show-buffer | xclip -selection clipboard >/dev/null';
	switchc -Tnormal
}

bind -Tcopy-mode-vi v send -X begin-selection
bind -Tcopy-mode-vi J send -X -N2 scroll-down
bind -Tcopy-mode-vi K send -X -N2 scroll-up
bind -Tcopy-mode-vi \{ send -X halfpage-up
bind -Tcopy-mode-vi \} send -X halfpage-down 
